//
//  VideoCollentionCell.h
//  LoveCar
//
//  Created by Xue Senhong on 15/5/21.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "VideoCollentionCell.h"
#import "VideoCell.h"
#import "NetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "MainNewsViewController.h"
#import "AutoScrollView.h"
#import "MJRefreshHeaderView.h"
#import "MJRefreshFooterView.h"
#import "MJRefresh.h"

@implementation VideoCollentionCell

- (void)dealloc
{
    [_tableView release];
    [_arr release];
    [_dic release];
    [_newArr release];
    [_index release];
    [_share release];
    [_art release];
    [_delegate release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.index = [NSString string];
        self.arr = [NSMutableArray array];
        self.dic = [NSMutableDictionary dictionary];
        self.newArr = [NSMutableArray array];
        
        // tableView
        self.tableView = [[UITableView alloc] init];
        [self.tableView setDelegate:self];
        [self.tableView setDataSource:self];
        [self.contentView addSubview:self.tableView];
        [self.tableView registerClass:[VideoCell class] forCellReuseIdentifier:@"reuse"];
        [_tableView release];
        
        [self setupRefresh];
        
    }
    return self;
}
// 重载cell数据的方法
- (void)reloadDataForCell
{
    // url数组
    NSString *video = @"http://app.api.autohome.com.cn/autov4.0/news/videos-a2-pm1-v4.0.0-vt8-p1-s20.html";
    NSString *oVideo = @"http://app.api.autohome.com.cn/autov4.0/news/videos-a2-pm1-v4.0.1-vt8-p1-s20.html";
    NSMutableArray *urlArr = [NSMutableArray arrayWithObjects:video, oVideo, nil];
    // 判断当前页选择不同的url加载数据
    
    __block VideoCollentionCell *videoCollentionCell = self;
    if ([self.index integerValue] == 1) {
        [NetworkHandler connectNetGETDataWithURLString:urlArr[0] completion:^(id result) {
            videoCollentionCell.arr = [[result objectForKey:@"result"] objectForKey:@"list"];
            videoCollentionCell.pageIndex = [[[result objectForKey:@"result"] objectForKey:@"pageindex"] integerValue];
            [videoCollentionCell.tableView reloadData];
        }];
    } else if ([self.index integerValue] == 11) {
        [NetworkHandler connectNetGETDataWithURLString:urlArr[1] completion:^(id result) {
            videoCollentionCell.arr = [[result objectForKey:@"result"] objectForKey:@"list"];
            videoCollentionCell.pageIndex = [[[result objectForKey:@"result"] objectForKey:@"pageindex"] integerValue];
            [videoCollentionCell.tableView reloadData];
        }];
    }
}
// 页面布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.tableView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 64 - 49)];
}

/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//#warning 自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
//    [self.arr removeAllObjects];
    [self reloadDataForCell];
    
    // 刷新表格
//    [self.tableView reloadData];
    
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [self.tableView headerEndRefreshing];
}

- (void)footerRereshing
{
    // url数组
    NSString *video = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/videos-a2-pm1-v4.0.0-vt8-p%ld-s20.html", self.pageIndex + 1];
    NSString *oVideo = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/videos-a2-pm1-v4.0.1-vt8-p%ld-s20.html", self.pageIndex + 1];
    NSMutableArray *urlArr = [NSMutableArray arrayWithObjects:video, oVideo, nil];
    
    __block VideoCollentionCell *videoCollentionCell = self;

    // 判断当前页选择不同的url加载数据
    if ([self.index integerValue] == 1) {
        [NetworkHandler connectNetGETDataWithURLString:urlArr[0] completion:^(id result) {
            videoCollentionCell.newArr = [[result objectForKey:@"result"] objectForKey:@"list"];
            videoCollentionCell.pageIndex = [[[result objectForKey:@"result"] objectForKey:@"pageindex"] integerValue];
            [videoCollentionCell.arr addObjectsFromArray:videoCollentionCell.newArr];
            [videoCollentionCell.tableView reloadData];
        }];
    } else if ([self.index integerValue] == 11) {
        [NetworkHandler connectNetGETDataWithURLString:urlArr[1] completion:^(id result) {
            videoCollentionCell.newArr = [[result objectForKey:@"result"] objectForKey:@"list"];
            videoCollentionCell.pageIndex = [[[result objectForKey:@"result"] objectForKey:@"pageindex"] integerValue];
            [videoCollentionCell.arr addObjectsFromArray:videoCollentionCell.newArr];
            [videoCollentionCell.tableView reloadData];
        }];
    }
    
    // 刷新表格
    [self.tableView reloadData];
    
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [self.tableView footerEndRefreshing];
}

#pragma mark - tableView的协议方法
// tableViewCell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
// cell实现方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    self.dic = [self.arr objectAtIndex:indexPath.row];
    // KVC赋值
    self.art = [[Article alloc] init];
    [self.art setValuesForKeysWithDictionary:self.dic];
    [cell valueforCellWithArticle:self.art];
    [_art release];
    return cell;
}
// cell的数量(每个section)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arr count];
}
// cell选则方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消cell选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 让代理人调用方法
    [self.delegate pushViewControllerWithString:[[self.arr objectAtIndex:indexPath.row] objectForKey:@"id"]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
